﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// WarehouseEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WarehouseEditWindow : Window
    {
        public Warehouse Warehouse { get; set; }

        private WarehouseService warehouseService = ServiceContainer.GetService<WarehouseService>();

        public WarehouseEditWindow()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.Warehouse == null)
            {
                this.Warehouse = new Warehouse { Enabled = true };
                this.Title = "增加仓库";
            }
            else
            {
                this.tbName.IsEnabled = false;
                this.Title = "编辑仓库";
            }

            this.tbName.Text = this.Warehouse.Name;
            this.tbAddress.Text = this.Warehouse.Address;
            this.tbPhone.Text = this.Warehouse.Phone;
            this.chkEnabled.IsChecked = this.Warehouse.Enabled;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = this.tbName.Text.Trim();
                string phone = this.tbPhone.Text.Trim();
                string address = this.tbAddress.Text.Trim();
                bool enabled = this.chkEnabled.IsChecked.Value;

                if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(address))
                {
                    MessageBox.Show("输入信息不能为空");
                    return;
                }

                this.Warehouse.Name = name;
                this.Warehouse.Phone = phone;
                this.Warehouse.Address = address;
                this.Warehouse.Enabled = enabled;

                if (this.Warehouse.Id < 1)
                {
                    this.Warehouse.Id = this.warehouseService.Insert(this.Warehouse);
                }
                else
                {
                    this.warehouseService.Update(this.Warehouse);
                }
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
